package PlaneGame.CODE;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameFrame extends JPanel {
    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
    }

    JFrame frame;
    KingThread kingThread =new KingThread();
        String[] backgroundPaths={"src/PlaneGame/IMAGE/background.png"};
    Background background=new Background(frameWidth,frameHeight,0,0,1,backgroundPaths);

    static int frameWidth=500,frameHeight=700;
    static boolean suspendFlag=false;
    static YaoBGM yaoBGM=new YaoBGM("src/PlaneGame/BGM/yao.wav");
    static StaticString tip1=new StaticString("上下左右控制方向",170,280);
    static StaticString tip2=new StaticString("空格射击",210,330);
        static String[] startButtonPaths={"src/PlaneGame/IMAGE/start.png"};
    static StartButton startButton=new StartButton(200,50,150,500,1,startButtonPaths);
        static String[] againButtonPaths={"src/PlaneGame/IMAGE/again.png"};
    static AgainButton againButton=new AgainButton(200,50,150,420,1,againButtonPaths);
    static ScoreString scoreString=new ScoreString("分数：",5,45);
    static BloodString bloodString=new BloodString("血量：",5,25);
    static BulletString bulletString=new BulletString("弹药：",5,65);
    static RecordString recordString=new RecordString("",160,250);
        static String[] suspendButtonPaths={"src/PlaneGame/IMAGE/pause_nor.png",
                "src/PlaneGame/IMAGE/pause_pressed.png",
                "src/PlaneGame/IMAGE/resume_nor.png",
                "src/PlaneGame/IMAGE/resume_pressed.png"};
    static SuspendButton suspendButton=new SuspendButton(40,40,425,15,4,suspendButtonPaths);
        static String[] exitButtonPaths={"src/PlaneGame/IMAGE/exit.png"};
    static ExitButton exitButton=new ExitButton(200,50,150,500,1,exitButtonPaths);
    static MedicineGenerator medicineGenerator=new MedicineGenerator();
    static AmmoGenerator ammoGenerator=new AmmoGenerator();
    static BombAmmoGenerator bombAmmoGenerator=new BombAmmoGenerator();
    static EnemyAGenerator enemyAGenerator=new EnemyAGenerator();
    static EnemyBGenerator enemyBGenerator=new EnemyBGenerator();
    static EnemyCGenerator enemyCGenerator=new EnemyCGenerator();
    static MrW mrW=new MrW();
    static GameData gameData=new GameData();

    GameFrame(){
        frame=new JFrame("飞机大战");
        frame.setSize(frameWidth,frameHeight);
        int locationX,locationY;
        Toolkit toolkit = Toolkit.getDefaultToolkit();//初始化工具包
        Dimension screenSize=toolkit.getScreenSize();//获取屏幕尺寸
        locationX=screenSize.width/2-frameWidth/2;
        locationY=screenSize.height/2-frameHeight/2;
        frame.setLocation(locationX,locationY);
        frame.add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.addKeyListener(new KeyMonitor());
        addMouseListener(new MouseMonitor());

        startThread();
    }
    void startThread(){
        new Thread(kingThread).start();
        new Thread(scoreString).start();
        new Thread(bloodString).start();
        new Thread(bulletString).start();
        new Thread(enemyAGenerator).start();
        new Thread(mrW).start();
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        background.drawMyself(g);
        tip1.drawMyself(g);
        tip2.drawMyself(g);
        medicineGenerator.drawVector(g);
        ammoGenerator.drawVector(g);
        bombAmmoGenerator.drawVector(g);
        enemyAGenerator.drawVector(g);
        enemyBGenerator.drawVector(g);
        enemyCGenerator.drawVector(g);
        mrW.drawMyself(g);
        enemyCGenerator.enemyC.winString.drawMyself(g);
        mrW.lostString.drawMyself(g);
        recordString.drawMyself(g);
        startButton.drawMyself(g);
        suspendButton.drawMyself(g);
        exitButton.drawMyself(g);
        againButton.drawMyself(g);
        scoreString.drawMyself(g);
        bloodString.drawMyself(g);
        bulletString.drawMyself(g);
    }

    static class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            mrW.turn(e);
            mrW.shot(e);
        }
        @Override
        public void keyReleased(KeyEvent e) {
            mrW.stop(e);
        }
    }

    class MouseMonitor extends MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e) {
            startButton.mouseClick(e);
            exitButton.mouseClick(e);
            againButton.mouseClick(e);
        }
        @Override
        public void mousePressed(MouseEvent e) {
            suspendButton.mousePressed(e);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            suspendButton.mouseReleased(e);
        }
    }

    class KingThread implements Runnable{
        boolean loop=true;
        int kingRate =10;//总帧率
        @Override
        public void run() {
            while (loop) {
                try {
                    Thread.sleep(kingRate);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                repaint();
            }
        }
    }
}